package net.dlz.example.controller;

import net.dlz.example.domain.Admin;
import net.dlz.example.domain.SearchAdminResponse;
import net.dlz.example.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/Admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/Alogin")
    public String Alogin(){return "admin_login";}
    @RequestMapping("/login")
    public String login(Model model, String name, String password, HttpSession session){
        Admin admin =adminService.finAdminByName(name);
        String msg = null;
        if(admin ==null){
            msg = "用户名不存在";
            model .addAttribute("msg",msg);
            return "admin_login";
        }else{
            String pwd = admin.getAdminPwd();
            if (pwd!=null && pwd.equals(password)){
                msg = "登录成功";
                model.addAttribute("msg",msg);
                session.setAttribute("current_user",admin);
                return "door_list1";
            }else{
                return "sorry";
            }
        }
    }
    @RequestMapping("/ulogin")
    public  String userLogin(){return "admin_login";}

    @RequestMapping("/sorry")
    public  String Sorry(){return "sorry";}

    @RequestMapping("/alist")
    public String alist(){return "admin_list";}

    @GetMapping("api/search")
    @ResponseBody
    public SearchAdminResponse searchAdminResponse(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        return adminService.findAllAdmin(pageNum,pageSize);
    }

    @GetMapping("/api/searchLink")
    @ResponseBody
    public SearchAdminResponse searchAdminResponse(@RequestParam("keywords") String keywords, @RequestParam("pageNum") Integer pageNum,
                                                   @RequestParam("pageSize") Integer pageSize){
        return adminService.findAdminLinkNmae(keywords,pageNum,pageSize);
    }
    @DeleteMapping("/delete")
    @ResponseBody
    public Boolean deleteAdmin(@RequestParam("id") String id){
        int deleted = adminService.deleteAdmin(id);
        return deleted > 0;
    }
}
